package cn.itsource.blog.service.impl;

import cn.itsource.blog.domain.Article;
import cn.itsource.blog.dto.ArticleDto;
import cn.itsource.blog.mapper.ArticleMapper;
import cn.itsource.blog.query.ArticleQuery;
import cn.itsource.blog.service.IArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.*;

/**
 * 业务实现类：文章Article
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<Article> findAll() {
        return articleMapper.findAll();
    }

    @Override
    public Article findOne(Long id) {
        return articleMapper.findOne(id);
    }

    @Transactional
    @Override
    public void add(Article article) {
        //将数据添加到mysql数据库
        article.setPublishId(1L);//没有做登录先写死测试
        articleMapper.add(article);

        //article - id值就有 - 后期做删除需要用它【_id没有】
        //将数据添加到mongodb
        mongoTemplate.save(article);
    }

    @Transactional
    @Override
    public void update(Article article) {
        //修改mysql中的数据
        article.setUpdateTime(new Date());
        articleMapper.update(article);

        //修改mongodb中的数据【article只有id，没有_id】
        Query query = new Query(Criteria.where("id").is(article.getId()));
        Article mongodbArticle = mongoTemplate.findOne(query,Article.class);
        if(mongodbArticle != null){
            article.set_id(mongodbArticle.get_id());
        }
        mongoTemplate.save(article);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        //删除mysql中的数据
        articleMapper.delete(id);
        //删除mongodb中的数据
        Query query = new Query(Criteria.where("id").is(id));
        mongoTemplate.remove(query,Article.class);
    }

    @Transactional
    @Override
    public void patchDel(Long[] ids) {
        articleMapper.patchDel();

        for (Long id : ids) {
            Query query = new Query(Criteria.where("id").is(id));
            mongoTemplate.remove(query,Article.class);
        }
    }

    @Override
    public PageInfo<Article> queryPage(ArticleQuery articleQuery) {
        //开启分页
        PageHelper.startPage(articleQuery.getCurrentPage(),articleQuery.getPageSize());
        //1.首先会查询总条数
        //2.会根据方言或驱动名进行分页查询数据 - mysql - limit
        //3.会自动计算limit的第一个参数

        List<Article> list = articleMapper.queryPage(articleQuery);
        //调用PageInfo的有参会计算其他参数
        PageInfo<Article> pageInfo = new PageInfo<Article>(list);
        return pageInfo;
    }

    @Override
    public List<ArticleDto> countByArticleType() {
        return articleMapper.countByArticleType();
    }

    @Override
    public List<ArticleDto> countByTime() {
        return articleMapper.countByTime();
    }

    @Override
    public List<Article> dependencyArticle(Long id) {
        return articleMapper.dependencyArticle(id);
    }
}
